/**
 * Created by wry on 2015/1/13.
 */

var reportService = require('../../services/backoffice/backOffice-reportService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');
var moment = require('moment');

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
function newIncrementDate(startDate, dayIncrement){
    var tempdate = new Date(startDate)
    var newdate = new Date(tempdate.setDate(tempdate.getDate() + dayIncrement));
    return newdate
}

exports.getBackOfficeCommunityMainStatsReport = function (req, res) {
    var community = req.params.community;

    reportService.getBackOfficeCommunityMainStatsService(community, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeCommunityMainStatsService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeCommunityMainStatsService...!' + err.message));
        } else {
            var data = {
                users: results[0],
                //vusers: results[1],
                question: results[1],
                questionusers: results[2],
                replies: results[3],
                repliesusers: results[4]
            }
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};

exports.getBackOfficeReport = function (req, res) {
    var community = req.params.community;
    var date = req.params.date;
    var column = req.params.column;
    var interval = req.params.interval;

    if(date == new Date(date).format("yyyy-MM-dd").toString())
        date = new Date(date).format("yyyy-MM-dd 23:59:59")

    reportService.getBackOfficeReportService(community, date, column, interval, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeReportService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeReportService...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}

exports.getBackOfficeRetentionByCommReport = function (req, res) {
    var community = req.params.community;
    var interval = req.params.interval;

    reportService.getBackOfficeRetentionByCommService(community, interval, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeRetentionByCommReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeRetentionByCommReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}

exports.getBackOfficeRetentionByCommAndBatchReport = function (req, res) {
    var community = req.params.community;
    var userBatchDate = req.params.userBatchDate;
    var interval = req.params.interval;


    reportService.getBackOfficeRetentionByCommAndBatchService(community, userBatchDate, interval, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeRetentionByCommAndBatchReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeRetentionByCommAndBatchReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}


exports.getBackOfficeMarketingInviteListReport = function(req, res){
    var community = req.params.community;

    reportService.getBackOfficeMarketingInviteListService(community, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeMarketingInviteListReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeMarketingInviteListReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}

exports.getBackOfficeMarketingInviteSentListReport = function(req, res){
    var community = req.params.community;

    reportService.getBackOfficeMarketingInviteSentListService(community, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeMarketingInviteSentListReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeMarketingInviteSentListReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}

exports.getBackOfficeClickSourceReport = function(req, res){
    var startDate = req.params.startDate;
    var endDate = req.params.endDate;

    reportService.getBackOfficeClickSourceService(startDate, endDate, function (err, results) {
        if (err) {
            logger.error('Exception happened when getBackOfficeClickSourceReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeClickSourceReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}